import 'package:ceramics/model/ab_singleton.dart';
import 'package:ceramics/util/ab_init_db.dart';
import 'package:ceramics/util/ab_value_converter.dart';

class ABLike {
  int? id;
  String likeId = '';
  String userId = '';

  ABLike({required this.likeId,required this.userId});

  ABLike.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    likeId = valueToString(json['like_id']);
    userId = valueToString(json['user_id']);
  }

  static Future<List<ABLike>> queryAllLikes() async {
    var list = await abDB.query('likes', orderBy: 'id desc' ,where: 'user_id = ${ABSingleton.user!.userId}');
    return list.map((e) => ABLike.fromJson(e)).toList();
  }

  static Future<void> insertLike(String likeId) async {
    var like = ABLike(likeId: likeId, userId: ABSingleton.user!.userId);
    await abDB.insert('likes', like.toJson());
  }

  static Future<void> deleteLike(String likeId) async {
    await abDB.delete('likes', where: 'like_id = ?', whereArgs: [likeId]);
  }

  Map<String, Object?> toJson() {
    return {
      'id': id,
      'like_id': likeId,
      'user_id': userId,
    };
  }

  static deleteLikesWithUserId(String s) {
    abDB.delete('likes', where: 'user_id = ?', whereArgs: [s]);
  }

}